﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using Do_an_C_sharp.DAL;

namespace Do_an_C_sharp.BLL
{
    class HangHoaController
    {
        DemoDataContext db = new DemoDataContext();
        public List<MatHang> layhang()
        {
            return db.MatHangs.ToList();
        }
        
        public List<MatHang> timma(string ma)
        {
            return db.MatHangs.Where(mh => mh.MAHANG == ma).ToList();
        }
        public List<MatHang> timten(string ten)
        {
            return db.MatHangs.Where(mh => mh.TENHANG.Contains(ten)).ToList();
        }
        public MatHang timelement(string ma)
        {
            return db.MatHangs.Where(mh => mh.MAHANG == ma).FirstOrDefault();
        }
        public void them(string mahang, string baohanh, string ten, int soluong, string mota, string nhasx, int dongia, string dvt)
        {
            //xây dựng chưa hoàn thiện
            MatHang hang = new MatHang();
            hang.MAHANG = mahang;
            hang.NHASX = nhasx;
            hang.SOLUONG = soluong;
            hang.MOTA = mota;
            hang.TENHANG = ten;
            hang.THONGTINBAOHANH = baohanh;
            hang.DGBAN = dongia;
            hang.DONVITINH = dvt;
            db.MatHangs.InsertOnSubmit(hang);
        }
        public void xoa(string ma)
        {
            db.MatHangs.DeleteOnSubmit(timelement(ma));
            //db.SubmitChanges();
        }
        public bool sua(string ma,string nhasx,int soluong,string dvt,string tenhang,string baohanh,int giaban, string mota)
        {
            //mới xây dựng để sửa mô tả thôi
            MatHang h = timelement(ma);
            if (h != null)
            {
                h.NHASX = nhasx;
                h.SOLUONG = soluong;
                h.DONVITINH = dvt;
                h.TENHANG = tenhang;
                h.THONGTINBAOHANH = baohanh;
                h.DGBAN = giaban;
                h.MOTA = mota;
                return true;
            }
            return false;
        }

        public bool capnhatsoluong(string ma, int slthem)
        {
            MatHang h = timelement(ma);
            if (h != null)
            {
                h.SOLUONG += slthem;
                return true;
            }
            return false;
        }

        public bool capnhatsoluongxoa(string ma, int slthem)
        {
            MatHang h = timelement(ma);
            if (h != null)
            {
                h.SOLUONG += slthem;
                db.SubmitChanges();
                return true;
            }
            return false;
        }

        public void luu()
        {
            db.SubmitChanges();
        }

        public MatHang gethangByten(string tenhang)
        {
            return db.MatHangs.Where(mh => mh.TENHANG == tenhang).FirstOrDefault();
        }
    }
}
